<?php

namespace addons\mental\controller;

use think\Db;


class Tuance extends Base
{
    protected $noNeedLogin = [];
    
    
    
    /**
     * 邀请码管理(addons/mental/tuance/lists)
     * @param string $mobile 手机号
     */
    public function lists()
    {
        
        $where = [];
        
        $mobile = $this->request->param('mobile');
        if ($mobile) {
            $where['u.mobile'] = $mobile;
        }else {
            $where['u.id'] = $this->auth->id;
        }
        
        $list = Db::name('mental_invitationcode')
            ->alias('ic')
            ->join('mental_testmain t','t.id=ic.main_id')
            ->join('user u','ic.user_id=u.id')
            ->where($where)
            ->order('ic.id DESC')
            ->field('ic.*,t.title')
            ->paginate(20);
        
        $this->success('成功',$list);
    }
    
    
    
    /**
     * 测试记录(addons/mental/tuance/testList)
     * @param string $invitation_code 邀请码管理
     * @param string $starttime 开始时间
     * @param string $endtime 结束时间
     */
    public function testList()
    {
        $where = ['tl.status'=>'finish'];
        $invitation_code = $this->request->param('invitation_code');
        if ($invitation_code) {
            $where['tl.invitation_code'] = $invitation_code;
        }
        
        $starttime = $this->request->param('starttime');
        $endtime = $this->request->param('endtime');
        if($starttime&&$endtime){
            
            $starttime = date('Y-m-d 00:00:00',strtotime($starttime));
            $endtime = date('Y-m-d 23:59:59',strtotime($endtime));
            $where['tl.updatetime'] = ['between time',[$starttime,$endtime]];
        }
        
        $list = Db::name('mental_testlog')
            ->alias('tl')
            ->join('mental_testmain t','t.id=tl.main_id')
            ->join('user u','tl.user_id=u.id')
            ->where($where)
            ->order('tl.id DESC')
            ->field('tl.id as test_id,u.nickname,u.mobile,t.title,tl.updatetime,tl.main_id')
            ->paginate(20);
        
        $this->success('成功',$list);
    }
    
}
